package leaf.science.pages
{
	import caurina.transitions.Tweener;
	
	import fl.motion.MotionEvent;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	
	import leaf.science.nav.Page;
	import leaf.science.ui.camera.Camera;
	import leaf.science.ui.jogwheel.JogWheel;
	import leaf.science.ui.leftmenu.LeftMenu;
	import leaf.science.ui.rightmenu.RightMenu;

	public class JogPageBase extends Page
	{
		public var leftMenu:LeftMenu;
		public var rightMenu:RightMenu;
		public var jogWheel:JogWheel;
		public var camera:Camera;
		
		protected var _prevPage:String;
		protected var _nextPage:String;
		protected var _message:String;
		protected var _data:XML;
		protected var _menuItems:Array;
		
		protected var _loader:Loader = new Loader();
		protected var _camSwfURL:String;
		
		public function JogPageBase()
		{
			super();
			init();
		}
		
		override public function goIn():void
		{
			_prevPage = LeafScience.instance.prevPageName;
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp)
			jogWheel.addEventListener(MotionEvent.MOTION_END, onJogInComplete);
			if (_prevPage != 'features' && _prevPage != 'demos')
			jogWheel.startTransitionIn() 
			else 
//			onJogInComplete();
			jogWheel.startTransitionIn();
		}
		
		override public function goOut():void
		{
			_nextPage = LeafScience.instance.nextPage.toString();
			if (camera) camera.stop();
			leftMenu.close();
			rightMenu.close();
			setTimeout(cameraOut, 300);
		}
		
		private function cameraOut ():void
		{
			Tweener.addTween(camera, {alpha:0, time:0.3});
			if (_nextPage == 'demos' || _nextPage == 'features')
			//setTimeout(onOutComplete, 300)
			setTimeout(jogOut, 300)
			else setTimeout(jogOut, 300);
		}
		
		private function jogOut ():void
		{
			_loader.unload();
			jogWheel.removeEventListener(MotionEvent.MOTION_END, onJogInComplete);
			jogWheel.addEventListener(MotionEvent.MOTION_END, onJogOutComplete);
			jogWheel.startTransitionOut();
		}
		
		private function onJogOutComplete (evt:Event):void
		{
			setTimeout(onOutComplete, 300);
		}
		
		private function onJogInComplete (evt:Event = null):void
		{
			leftMenu.open();
			jogWheel.addEventListener( Event.CHANGE, onJogWheelChange );
			setTimeout(loadCamera, 300);
		}
		
		private function loadCamera ():void
		{
			_loader.load( new URLRequest( _camSwfURL ));
		}
		
		private function onCameraComplete(evt:Event):void
		{
			rightMenu.open();
			camera.addEventListener( MouseEvent.CLICK, onCameraClick );
			
		}
		
		
		protected function onRightSelect (evt:Event):void
		{
			// what to do on right menu select
		}
	
		private function onLoadInit( evt:Event ):void
		{
			registerCameraType();
			camera.alpha = 0;
			camera.addEventListener( Event.CHANGE, onCameraChange );
			camera.addEventListener(Event.COMPLETE, onCameraComplete);
			camera.x = -Math.round( camera.width/2 );
			camera.y = -Math.round( camera.height/2 );
			Tweener.addTween(camera, {alpha:1, time:0.6, onComplete:camera.spin, onCompleteParams:[30]});
		}
		
		protected function registerCameraType():void
		{
			// diff between cameras
		}
		
		private function onLoadProgress( evt:ProgressEvent ):void
		{
			jogWheel.range.end = evt.bytesLoaded/evt.bytesTotal;
		}
	
		private function onJogWheelChange( evt:Event ):void
		{
			camera.frame = Math.round( camera.totalFrames * jogWheel.value );
		}
		
		private function onCameraChange( evt:Event ):void
		{
			jogWheel.value = camera.angle/360;
		}
		
		protected function onCameraClick (evt:MouseEvent):void
		{
			// what to do on camera click
		}
		
		protected function onKeyUp(evt:KeyboardEvent):void
		{
			
		}
		
		protected function init ():void
		{
			leftMenu = new LeftMenu();
			leftMenu.selectedName = this.toString();
			addChild(leftMenu);
			
			rightMenu = new RightMenu(_message);
			addChild(rightMenu);
			
			rightMenu.addEventListener(Event.CHANGE, onRightSelect);
			rightMenu.data = _menuItems;
			
			jogWheel = new JogWheel();
			jogWheel.alpha = 0;
			addChild( jogWheel );
	
			_loader = new Loader;
			_loader.mouseEnabled = false;
			_loader.contentLoaderInfo.addEventListener( Event.INIT, onLoadInit );
			_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onLoadProgress );
			addChild( _loader );
			
		}
		
	}
}